package com.selfdriving.activity;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.selfdriving.BaseFragmentActivity;
import com.selfdriving.R;
import com.selfdriving.dialog.ChooseDialog;
import com.selfdriving.dialog.ChooseDialog.setOnDialogTwoClickListener;
import com.selfdriving.utils.Constant;
import com.selfdriving.utils.ViewFlipperView;

public class ActivityScenicExamine extends BaseFragmentActivity implements
		OnClickListener
{
	LinearLayout viewFlipperRL;
	LinearLayout Lradio;
	TextView topText;
	ImageView topBack;
	TextView round;
	TextView goOver;
	TextView wantGo;
	TextView content;
	int x = 0;
	String imgs[];
	String sid;
	Button sendComment;
	ChooseDialog dialog;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_scenic_examine);
		initViews();
		initEvents();
		Intent intent = super.getIntent();
		Bundle bundle = intent.getBundleExtra("bundle");
		String name = bundle.getString("name");
		sid = bundle.getString("sid");
		String msg = bundle.getString("content");
		String arrived = bundle.getString("arrived");
		String wantGot = bundle.getString("wantGo");
		imgs = bundle.getStringArray("imgs");
		content.setText(msg);
		wantGo.setText(wantGot);
		goOver.setText(arrived);
		topText.setText(name);
		if (viewFlipperRL.getChildCount() > 0)
		{
			viewFlipperRL.removeAllViews();
		}
		ViewFlipperView flipperView = new ViewFlipperView(mApplication,
				ActivityScenicExamine.this, round, Lradio, 0, imgs);
		viewFlipperRL.addView(flipperView.viewFlipper, layoutParams);
	}

	@Override
	protected void initViews()
	{

		sendComment = (Button) findViewById(R.id.top_right_but);
		viewFlipperRL = (LinearLayout) findViewById(R.id.vf_layout);
		Lradio = (LinearLayout) findViewById(R.id.radio_layout);
		topText = (TextView) findViewById(R.id.top_text_middle);
		round = (TextView) findViewById(R.id.viewflipper_size);
		topBack = (ImageView) findViewById(R.id.head_break);
		goOver = (TextView) findViewById(R.id.goOver);
		wantGo = (TextView) findViewById(R.id.wantGo);
		content = (TextView) findViewById(R.id.content);
		topBack.setVisibility(View.VISIBLE);
		sendComment.setVisibility(View.VISIBLE);
		sendComment.setText("发布评论");
	}

	@Override
	protected void initEvents()
	{
		// TODO Auto-generated method stub
		topBack.setOnClickListener(this);
		goOver.setOnClickListener(this);
		wantGo.setOnClickListener(this);
		sendComment.setOnClickListener(this);

	}

	public void sendComment(String content, String starNum)
	{
		if (mApplication.user == null)
		{
			showShortToast("您还未登录,请登录后再评论");
			return;
		}
		String phone = mApplication.user.getPhone();
		showLoadingDialog("请求发送中...");
		AjaxParams params = new AjaxParams();
		params.put("f", "38");
		params.put("phone", phone);
		params.put("sid", sid);
		params.put("star", starNum);
		params.put("txt", content);
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				dismissLoadingDialog();
			}

			@Override
			public void onSuccess(Object o)
			{
				System.out.println(o.toString());
				if (o.toString().contains("000000"))
				{
					showShortToast("评论成功！谢谢您的参与");
				}
				dismissLoadingDialog();
			}
		});
	}

	public void gotoScenic(final String host)
	{
		showLoadingDialog("请求发送中...");
		AjaxParams params = new AjaxParams();
		params.put("f", host);
		params.put("sid", sid);
		FinalHttp fh = new FinalHttp();
		fh.configTimeout(10000);
		fh.post(Constant.POST, params, new AjaxCallBack<Object>()
		{
			@Override
			public void onFailure(Throwable t, String strMsg)
			{
				dismissLoadingDialog();
			}

			@Override
			public void onSuccess(Object o)
			{
				if (o.toString().contains("000000"))
				{
					if (host.equals("26"))
					{
						int x = Integer.parseInt(goOver.getText().toString());
						goOver.setText("" + (x + 1));
					} else if (host.equals("27"))
					{
						int x = Integer.parseInt(wantGo.getText().toString());
						wantGo.setText("" + (x + 1));
					}
				}
				dismissLoadingDialog();
			}
		});
	}

	public void setText(View v)
	{
		Button layout = (Button) v;
		final String title = layout.getText().toString();
		dialog = new ChooseDialog(this, title, "");
		dialog.setOnDialogTwoClickListener(new setOnDialogTwoClickListener()
		{
			@Override
			public void OnOkClick(String st, String st1)
			{
				// TODO Auto-generated method stub
				if (st != null && !st.equals(""))
				{
					System.out.println(st);
					sendComment(st, st1);
				}
				dialog.dismiss();
			}

			@Override
			public void OnCancelClick()
			{
				dialog.dismiss();
			}
		});
		dialog.show();
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId())
		{
		case R.id.head_break:
			finish();
			break;
		case R.id.goOver:
			gotoScenic("26");
			break;
		case R.id.wantGo:
			gotoScenic("27");
			break;
		case R.id.top_right_but:
			setText(v);
			break;
		}
	}

}
